<template>
	<view class="showApp">
		<view><u-toast ref="uToast" /></view>
		<view class="header_switc box box-align-center box-pack-between pad_def ">
			<view class="left">
				<view style="color: #fff; font-size: 30rpx;">
					自主应用上传
				</view>
				<view class="sub_tit">
					准荐在教管壹-学习应用库中查找需要推送安装的应用。若学习立用库没有，您可以点击下面按键进行自主上传并推送安装。
				</view>
				<view style="display: flex; justify-content: flex-end; padding: 10rpx;">
					<view style="color: #fff;" @click="goToUrl">我要上传</view>
				</view>
			</view>
		</view>
		<text style="margin-left: 20rpx; font-size: 30rpx;">教管壹-学习应用库</text>
		<view class="search">
			<u-search placeholder="输入关键字搜索" v-model="app_name" shape="round" :clearabled="true" :show-action="true" action-text="搜索" :action-style="actionStyle" :animation="false" border-color="#e6e6e6" @custom="toSearchApp">
			</u-search>
			<u-button size="medium" class="custom-style" @click="getAllApp()">全部应用</u-button>
		</view>
		<view v-if="showSubsection">
			<u-subsection :list="categoryList" :current="curNow" @change="sectionChange"></u-subsection>
		</view>
		
		<view class="work_time">
			<view class="work_top box_radius" v-for="(item, index) in list" :key="index"  @longpress.native="toDelApp(item.app_id)">
				<view class="box box-pack-between box-align-center">
					<view class="left" style="width: 85%;" @click="getAppInfo(item)">
						<image :src="item.app_img" class="work_top clockImg"></image>
						<view style="padding-top: 33rpx; font-weight: bolder;">{{item.app_name}}</view>
						<view style="padding: 5rpx; color: #bfbfbf;">{{item.app_desc}}</view>
						<view style="color: #bfbfbf;">{{item.app_size|zhDate}}/{{item.app_downloads ? item.app_downloads : 0}}次下载</view>
					</view>
					<view class="right">
						<view class="pushText">
							<text style="color: #3377ff;" @click="pushApp(item)">推送</text>
							<text style="float: right; color: #00ff00;" @click="installApp(item)">安装</text>
						</view>
					</view>
				</view>
			</view>
			<u-line style="margin-top: 20rpx;" />
		</view>
		<view>
			<u-modal v-model="showDel" content="删除app平板将同步卸载,是否确定删除?" @confirm="delSelfAppControl" :show-cancel-button="true" ></u-modal>
		</view>
	</view>
</template>

<script>
	import {
		searchAppControl,
		pushAppControl,
		installAppControl,
		delSelfApp
	} from '@/api/userHome.js'
import { getCateGory } from '../../../api/application'
	export default {
		data() {
			return {
				app_name: "",
				startSize: 1,
				isMore: false,
				actionStyle: {
					font_size: '120px',
					color:'#2979ff'
				},
				list: [],
				categoryList:[],
				showSubsection: false,
				curNow: 0,
				categoryId: 0,
				showDel: false,
				delAppId: 0
			}
		},
		onLoad() {
			this.getMiniCategory()
			setTimeout(()=>{
				let param = {
					userId: uni.getStorageSync('userId'),
					startSize: this.startSize,
					pageSize: 1000,
					app_category_id: this.categoryId
				}
				this.getSearchApp(param)
			}, 500)
		},
		methods: {
			toSearchApp(e) {
				let param = {
					app_name: e,
					startSize: 1,
					pageSize: 1000,
					userId: uni.getStorageSync('userId'),
					app_category_id: this.categoryId
				}
				this.getSearchApp(param)
			},
			getSearchApp(param){
				searchAppControl(param).then((res) => {
					this.list = res.data
					this.isMore = this.list.length == 10
				}).catch((err) => {
					this.list = []
				})
			},
			getAppInfo(item) {
				// let obj = {
				// 	app_id: item.app_id,
				// 	app_name: item.app_name,
				// 	app_img: item.app_img,
				// 	app_desc: item.app_desc,
				// 	app_downloads: item.app_downloads,
				// 	app_size: item.app_size
				// }
				// let info = encodeURIComponent(JSON.stringify(obj))
				// uni.navigateTo({
				// 	url: '/pages_subpackage/appControl/appInfo/index?info=' + info
				// })
				uni.navigateTo({
					url: '/pages_subpackage/appDetails/index?appId=' + item.app_id
				})
			},
			pushApp(item) {
				let param = {
					id: uni.getStorageSync('snId'),
					userId: uni.getStorageSync('userId'),
					lastIp: uni.getStorageSync('Ip'),
					app_id: item.app_id
				}
				pushAppControl(param).then(res => {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'success',
						icon: false
					})
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.msg,
						type: 'error',
						icon: false
					})
				})
			},
			installApp(item) {
				let param = {
					id: uni.getStorageSync('snId'),
					userId: uni.getStorageSync('userId'),
					lastIp: uni.getStorageSync('Ip'),
					app_id: item.app_id
				}
				installAppControl(param).then(res => {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'success',
						icon: false
					})
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.msg,
						type: 'error',
						icon: false
					})
				})
			},
			sectionChange(index) {
				this.curNow = index;
				this.categoryId = this.categoryList[index].id
				let param = {
					userId: uni.getStorageSync('userId'),
					startSize: this.startSize,
					pageSize: 1000,
					app_category_id: this.categoryId
				}
				this.getSearchApp(param)
			},
			goToUrl() {
				uni.navigateTo({
					url: '/pages_subpackage/appControl/addSelfApp/index'
				})
			},
			getMiniCategory() {
				getCateGory().then(res => {
					this.categoryList = res.data
					this.showSubsection = true
					if (this.categoryList.length > 0) {
						this.categoryId = this.categoryList[0].id
					}
				}).catch(err => {
					this.showSubsection = false
				})
			},
			getAllApp() {
				let param = {
					startSize: 1,
					pageSize: 1000,
					userId: uni.getStorageSync('userId')
				}
				this.getSearchApp(param)
			},
			toDelApp(appId) {
				this.showDel = true
				this.delAppId = appId
				console.log(this.delAppId)
			},
			delSelfAppControl() {
				delSelfApp({
					app_id: this.delAppId,
					id: uni.getStorageSync('snId'),
					last_ip: uni.getStorageSync('Ip'),
					userId: uni.getStorageSync('userId')
				}).then(res => {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'success',
						icon: false
					})
					setTimeout(()=> {
						let param = {
							startSize: 1,
							pageSize: 1000,
							userId: uni.getStorageSync('userId'),
							app_category_id: this.categoryId
						}
						this.getSearchApp(param)
					})
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.msg,
						type: 'error',
						icon: false
					})
				})
				
			}
			
		}
	}
</script>


<style scoped>
	.u-size-medium {
			display: inline-flex;
			width: auto;
			font-size: 26rpx;
			height: 70rpx;
			line-height: 70rpx;
			padding-right: 40rpx;
	}
</style>
<style lang="scss">
	.header_switc{
		border-radius: 30rpx;
		background: #5070b9;
		padding: 20rpx 20rpx;
		margin-bottom: 20rpx;
		.left{
			.tit{
				font-size: 30rpx;
				color: #fff;
			}
			.sub_tit{
				font-size: 24rpx;
				color: #7d9ce1;
			}
		}
	}
	.showApp {
		background-color: #fff;
		border-radius: 30rpx;
		.search {
			display: flex;
			margin: 20rpx;
		}
	}
	
	.work_top {
		background-color: #FFFFFF;
		padding: 30rpx;
		.footer_time {
			margin-top: 20rpx;
			font-size: 20rpx;
			letter-spacing: 2rpx;
		}
		.clockImg {
			height: 150rpx;
			width: 150rpx;
			float: left;
			padding: 0rpx;
			padding-right: 10rpx;
		}
		.pushText {
			    border-style: solid;
			    border-width: 1rpx;
			    border-radius: 27rpx;
			    width: 200rpx;
			    height: 74rpx;
			    padding: 17rpx;
			
		}
		
	}
	
	
</style>